﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;

namespace Shared
{
    public static class GarbageCollection
    {
        #region Fields

        /// <summary>
        /// Collection handling thread.
        /// </summary>
        private static Thread CollectionHandler = new Thread(new ThreadStart(CollectionProcess));

        #endregion

        #region Methods

        /// <summary>
        /// Starts the garbage collector.
        /// </summary>
        public static void Start()
        {
            CollectionHandler.Name = "GarbageCollector";
            CollectionHandler.Start();
        }

        /// <summary>
        /// Stops the garbage collector.
        /// </summary>
        public static void Stop()
        {
            CollectionHandler.Abort();
        }

        /// <summary>
        /// Process for the collection handler thread.
        /// </summary>
        private static void CollectionProcess()
        {
            try
            {
                while (true)
                {
                    GC.WaitForPendingFinalizers();
                    GC.Collect();

                    Thread.Sleep(5000);
                }
            }
            catch { }
        }

        #endregion
    }
}
